from pydantic.v1 import BaseSettings


class Db(BaseSettings):
    host: str
    port: int
    name: str
    user: str
    password: str
    echo: bool = False

    @property
    def full_url(self) -> str:
        return f"postgresql+asyncpg://{self.user}:{self.password}@{self.host}:{self.port}/{self.name}"


class RedisConfig(BaseSettings):
    host: str
    port: int
    database: int

    @property
    def url(self) -> str:
        return f"redis://{self.host}:{self.port}/{self.database}"


class ApiConfig(BaseSettings):
    host: str
    port: int
    frontend_url: str
    debug: bool = False


class AuthConfig(BaseSettings):
    secret_key: str
    token_expire_minutes: int


class SMSApiConfig(BaseSettings):
    login: str
    password: str


class Settings(BaseSettings):
    db: Db
    redis: RedisConfig
    api: ApiConfig
    auth: AuthConfig
    sms_api: SMSApiConfig

    class Config:
        env_file = ".env"
        env_file_encoding = "utf-8"
        env_nested_delimiter = "__"


def load_config(env_file=".env") -> Settings:
    settings = Settings(_env_file=env_file)
    return settings
